package com.changgou;

import org.csource.common.MyException;
import org.csource.fastdfs.*;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.io.FileOutputStream;
import java.io.IOException;

/**
 * @author liyipeng
 * @date 2021-06-26 22:23
 */
@SpringBootTest
@RunWith(SpringRunner.class)
public class Demo {


    /**
     * 上传图片到服务器
     */
    @Test
    public void uploadFile() throws MyException, IOException {
        //初始化配置
        ClientGlobal.init("D:\\JavaSenior\\IntelliJIDEA\\projects\\changgou\\changgou-parent\\changgou-service\\changgou-service-file\\src\\main\\resources\\fdfs_client.conf");
        //创建TrackerClient客户端对象
        TrackerClient trackerClient = new TrackerClient();
        //通过TrackerClient对象获取TrackerServer信息
        TrackerServer trackerServer = trackerClient.getConnection();
        //获取StorageClient对象
        StorageClient storageClient = new StorageClient(trackerServer, null);
        //执行文件上传
        String[] jpgs = storageClient.upload_file("D:\\codeTemp.jpg", "jpg", null);
        for (String jpg : jpgs) {
            System.out.println(jpg);
            //group1
            //M00/00/00/wKjThGDXsBKAKQRaAAAz5s1bxmA562.jpg
        }
    }


    /**
     * 下载文件
     */
    @Test
    public void downloadFile() throws MyException, IOException {
        ClientGlobal.init("D:\\JavaSenior\\IntelliJIDEA\\projects\\changgou\\changgou-parent\\changgou-service\\changgou-service-file\\src\\main\\resources\\fdfs_client.conf");
        TrackerClient trackerClient = new TrackerClient();
        TrackerServer trackerServer = trackerClient.getConnection();
        StorageClient storageClient = new StorageClient(trackerServer, null);

        byte[] bytes = storageClient.download_file("group1", "M00/00/00/wKjThGDXsBKAKQRaAAAz5s1bxmA562.jpg");
        FileOutputStream fos = new FileOutputStream("D:\\zzz.jpg");
        fos.write(bytes);
        fos.close();
    }

    /**
     * 删除文件
     */
    @Test
    public void deleteFile() throws MyException, IOException {
        ClientGlobal.init("D:\\JavaSenior\\IntelliJIDEA\\projects\\changgou\\changgou-parent\\changgou-service\\changgou-service-file\\src\\main\\resources\\fdfs_client.conf");
        TrackerClient trackerClient = new TrackerClient();
        TrackerServer trackerServer = trackerClient.getConnection();
        StorageClient storageClient = new StorageClient(trackerServer, null);

        int group1 = storageClient.delete_file("group1", "M00/00/00/wKjThGDXsBKAKQRaAAAz5s1bxmA562.jpg");
        System.out.println("删除图片是否成功");
    }


    /**
     * 获取文件的信息
     */
    @Test
    public void getFileInfo() throws MyException, IOException {
        ClientGlobal.init("D:\\JavaSenior\\IntelliJIDEA\\projects\\changgou\\changgou-parent\\changgou-service\\changgou-service-file\\src\\main\\resources\\fdfs_client.conf");
        TrackerClient trackerClient = new TrackerClient();
        TrackerServer trackerServer = trackerClient.getConnection();
        StorageClient storageClient = new StorageClient(trackerServer, null);

        FileInfo group1 = storageClient.get_file_info("group1", "M00/00/00/wKjThGDXsBKAKQRaAAAz5s1bxmA562.jpg");
        System.out.println(group1.toString());
        //source_ip_addr = 192.168.211.132, file_size = 13286, create_timestamp = 2021-06-27 06:54:10, crc32 = -849623456
    }
}
